import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: a
 * Date: 2022-07-17
 * Time: 16:39
 */
public class JDBC_Test1 {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource=new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/ebook?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");

        Connection connection= dataSource.getConnection();

        String sql="insert into borrow_info(book_id,student_id,start_time,end_time) select book.id,student.id,?,? from book,student where book.name=? and student.name=?";

        PreparedStatement statement=connection.prepareStatement(sql);
        statement.setTimestamp(1, Timestamp.valueOf("2019-09-25 17:50:00"));
        statement.setTimestamp(2, Timestamp.valueOf("2019-10-25 17:50:00"));
        statement.setString(3,"诗经");
        statement.setString(4,"貂蝉");

        int n=statement.executeUpdate();
        System.out.println("n="+n);

        statement.close();
        connection.close();
    }
}
